package com.example.habits;
import java.util.ArrayList;

import com.example.habits.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	Button showAllButton;
	Button showTodayButton;
	Button showWeeklyButton;
	Button deleteButton;
	TextView databaseText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Identifies the components in the view
		showAllButton = (Button)findViewById(R.id.showAllButton);
		showAllButton.setTextSize(10);
		
		showTodayButton = (Button)findViewById(R.id.showTodayButton);
		showTodayButton.setTextSize(10);
		
		showWeeklyButton = (Button)findViewById(R.id.showWeeklyButton);
		showWeeklyButton.setTextSize(10);
		
		deleteButton = (Button)findViewById(R.id.deleteButton);
		deleteButton.setTextSize(10);
		
		databaseText = (TextView)findViewById(R.id.databaseText);
		databaseText.setMovementMethod(new ScrollingMovementMethod());
		databaseText.setTextSize(12);
		

		// Creating button listener
		OnClickListener showAllButtonListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				EntryDataSource eds = EntryDataSource.getInstance();
				eds.open();
				
				ArrayList<Long> exactList = new ArrayList<Long>(eds.getAllExactEntries());
				ArrayList<String> stringList = new ArrayList<String>(eds.getAllEntries());
				ArrayList<Integer> weekList = new ArrayList<Integer>(eds.getAllWeeks());
				StringBuilder sb = new StringBuilder();
				
				for (int i = 0; i < stringList.size(); i++) {
				    sb.append(stringList.get(i) + "  " + exactList.get(i) + " " + weekList.get(i) + "\n");
				}
				
				databaseText.setText(sb.toString());
				
				eds.close();
			}
		};

		// Creating button listener
		OnClickListener showTodayButtonListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				EntryDataSource eds = EntryDataSource.getInstance();
				eds.open();
				
				ArrayList<String> entryList = new ArrayList<String>(eds.getTodaysEntries());
				StringBuilder sb = new StringBuilder();
				
				for (int i = 0; i < entryList.size(); i++) {
				    sb.append(entryList.get(i) + "\n");
				}
				
				databaseText.setText(sb.toString());
				
				eds.close();
			}
		};
		
		// Creating button listener
		OnClickListener showWeekButtonListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				EntryDataSource eds = EntryDataSource.getInstance();
				eds.open();
				
				ArrayList<String> entryList = new ArrayList<String>(eds.getWeeklyEntries());
				StringBuilder sb = new StringBuilder();
				
				for (int i = 0; i < entryList.size(); i++) {
				    sb.append(entryList.get(i) + "\n");
				}
				
				databaseText.setText(sb.toString());
				
				eds.close();
			}
		};
		
		// Creating button listener
		OnClickListener deleteButtonListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				EntryDataSource eds = EntryDataSource.getInstance();
				eds.open();
				
				eds.deleteAllEntries();
				
				eds.close();
				
				databaseText.setText("TABLE EMPTY");
			}
		};

		// Sets the listeners to corresponding button
		showAllButton.setOnClickListener(showAllButtonListener);
		showTodayButton.setOnClickListener(showTodayButtonListener);
		showWeeklyButton.setOnClickListener(showWeekButtonListener);
		deleteButton.setOnClickListener(deleteButtonListener);

		// Starts the background service
		startService(new Intent(getBaseContext(), BackgroundService.class));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
